﻿namespace WMS6.Web.Billing
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;
    using WMS6.Web.UserControls;

    public class RateList : BasePage
    {
        protected Button btnDelete;
        protected Button btnEdit;
        protected Button btnExport;
        protected Button btnList;
        protected Button btnNew;
        protected Button btnNewSearch;
        protected Button btnReSearch;
        protected HtmlForm F9052;
        protected GridView gvRate;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnRtnValue;
        protected HtmlHead Head1;
        protected Label lblMsg;
        protected Label lblUom;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindData2RateEntityList(RateQueryEntity rateQueryEntity)
        {
            this.PagerFooter.CurrentPage = int.Parse(rateQueryEntity.CurrentPage);
            DataSet ds = new DataSet();
            ds = BaseInfoServiceClientProxy.GetInstance().GetInfoByQueryEntity(rateQueryEntity, "RATE");
            base.BindData2GridView(this.gvRate, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> selectedID = base.GetSelectedIDsInGridView(this.gvRate, "RATE_ID");
                if ((selectedID == null) || (selectedID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F9150_010");
                }
                else
                {
                    this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Delete(selectedID, "RATE") + base.GetGlobalMsgResource("F9150_008");
                    RateQueryEntity rateQueryEntity = this.GetrateQueryEntity();
                    this.BindData2RateEntityList(rateQueryEntity);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9150_009") + ex.Message;
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvRate, "RATE_ID");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9150_010");
            }
            else if (selectedID.Count > 1)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9150_019");
            }
            else
            {
                base.Response.Redirect(string.Format("Rate.aspx?from=list&ID={0}", base.UrlEncode(selectedID[0])));
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            RateQueryEntity rateQueryEntity = this.GetrateQueryEntity();
            rateQueryEntity.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            string currentPageIndex = rateQueryEntity.CurrentPage;
            rateQueryEntity.CurrentPage = "0";
            this.gvRate.Columns[1].Visible = false;
            this.gvRate.AllowSorting = false;
            this.BindData2RateEntityList(rateQueryEntity);
            this.gvRate.ShowFooter = false;
            this.hdnActionID.Value = "";
            rateQueryEntity.IsGetAll = false;
            rateQueryEntity.CurrentPage = currentPageIndex;
            this.SetRateQueryEntity(rateQueryEntity);
            base.Export2Excel("Rate.xls", this.gvRate);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            RateQueryEntity rateQueryEntity = this.GetrateQueryEntity();
            rateQueryEntity.CurrentPage = "0";
            this.BindData2RateEntityList(rateQueryEntity);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            RateQueryEntity rateQueryEntity = this.GetrateQueryEntity();
            rateQueryEntity.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2RateEntityList(rateQueryEntity);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            RateQueryEntity rateQueryEntity = this.GetrateQueryEntity();
            rateQueryEntity.RateID = "";
            rateQueryEntity.RateName = "";
            rateQueryEntity.IsActive = "";
            this.BindData2RateEntityList(rateQueryEntity);
        }

        protected void btnNewSearch_Click(object sender, EventArgs e)
        {
            base.Server.Transfer("RateQuery.aspx");
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            RateQueryEntity rateQueryEntity = this.GetrateQueryEntity();
            if (int.Parse(rateQueryEntity.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                rateQueryEntity.CurrentPage = (int.Parse(rateQueryEntity.CurrentPage) + 1).ToString();
                this.BindData2RateEntityList(rateQueryEntity);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            RateQueryEntity rateQueryEntity = this.GetrateQueryEntity();
            if (int.Parse(rateQueryEntity.CurrentPage) > 0)
            {
                rateQueryEntity.CurrentPage = (int.Parse(rateQueryEntity.CurrentPage) - 1).ToString();
                this.BindData2RateEntityList(rateQueryEntity);
            }
        }

        private RateQueryEntity GetrateQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as RateQueryEntity);
            }
            return (base.PageQueryEntitySession as RateQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                RateQueryEntity rateQueryEntity = this.GetrateQueryEntity();
                rateQueryEntity.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2RateEntityList(rateQueryEntity);
            }
        }

        protected void gvRate_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F9052", this.gvRate);
        }

        protected void gvRate_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
                base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblDescr"));
            }
        }

        protected void gvRate_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            string strValue = base.UrlReplace(this.gvRate.DataKeys[e.NewSelectedIndex]["RATE_ID"].ToString());
            if (!string.IsNullOrEmpty(strValue))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'> window.returnValue ='" + strValue + "';  window.close();</script>", false);
            }
        }

        protected void gvRate_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                RateQueryEntity rateQueryEntity = this.GetrateQueryEntity();
                rateQueryEntity.SortField = e.SortExpression;
                if (rateQueryEntity.SortDirection == "ASC")
                {
                    rateQueryEntity.SortDirection = "DESC";
                }
                else if (rateQueryEntity.SortDirection == "DESC")
                {
                    rateQueryEntity.SortDirection = "ASC";
                }
                this.BindData2RateEntityList(rateQueryEntity);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            RateQueryEntity rateQueryEntity = new RateQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    rateQueryEntity.RateID = this.PreviousPage.RateID;
                    rateQueryEntity.RateName = this.PreviousPage.RateName;
                    rateQueryEntity.IsActive = this.PreviousPage.IsActive;
                }
                else
                {
                    rateQueryEntity.RateID = "";
                    rateQueryEntity.RateName = "";
                    rateQueryEntity.IsActive = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            rateQueryEntity.IsGetAll = false;
            rateQueryEntity.CurrentPage = "0";
            rateQueryEntity.PageSize = this.gvRate.PageSize.ToString();
            rateQueryEntity.SortField = "ROWID";
            rateQueryEntity.SortDirection = "ASC";
            this.PagerFooter.PageSize = this.gvRate.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["action"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["action"].ToString();
                this.gvRate.Columns[1].Visible = false;
                this.gvRate.Columns[2].Visible = true;
                this.btnNew.Visible = false;
                this.btnEdit.Visible = false;
                this.btnDelete.Visible = false;
                this.btnReSearch.Visible = false;
                this.btnExport.Visible = false;
            }
            else if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetRateQueryEntity(rateQueryEntity);
            this.BindData2RateEntityList(rateQueryEntity);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.lblMsg.Text = "";
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetRateQueryEntity(RateQueryEntity rateQueryEntity)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = rateQueryEntity;
            }
            else
            {
                base.PageQueryEntitySession = rateQueryEntity;
            }
        }

        public RateQuery PreviousPage
        {
            get
            {
                return (RateQuery) base.PreviousPage;
            }
        }
    }
}

